﻿
@{
    ViewBag.Title = "集采批次维护";
    List<NCP.Model.GroupPurchase> list = ViewData["gplist"] as List<NCP.Model.GroupPurchase>;
}

<div class="layui-fluid" id="LAY-gp-index">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">批次筛选</label>
                    <div class="layui-input-inline" style="width:100px">
                        @*@Html.RelGroupPurchaseSelect("GroupPurchase", "")*@

                        <select id="setpc" name="setpc" lay-filter="setpc" lay-verify="required" lay-search>
                            <option value="0">全部</option>
                            <option value="1">正在执行</option>
                            <option value="2">已到期</option>
                        </select>

                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">集采批次</label>
                    <div class="layui-input-inline" style="width:250px">
                        @*@Html.RelGroupPurchaseSelect("GroupPurchase", "")*@
                        <select name="GroupPurchase" id="GroupPurchase" lay-filter="GroupPurchase" lay-verify="required">
                            <option value="ALL">全部</option>
                            @foreach (var item in list)
                            {
                                <option value=@item.Code>@item.Year @item.Name</option>
                            }
                        </select>
                    </div>
                </div>
                
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-gp-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-gp-list" lay-filter="LAY-gp-list"></table>
            <script type="text/html" id="table-toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                </div>
            </script>
            <script type="text/html" id="barBtn">
                <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-sm" lay-event="drug">集采药品</a>
                <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

<script type="text/javascript">
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'admin'], function () {
        var table = layui.table
            , form = layui.form
            , $ = layui.$
            , admin = layui.admin;
        
        //数据绑定
        table.render({
            elem: '#LAY-gp-list'
            , url: '/GroupPurchase/GetGPList' //模拟接口
            , toolbar: '#table-toolbar'
            , defaultToolbar: ['filter' ,{
                title: '提示' //标题
                , layEvent: 'LAYTABLE_EXPORTS' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-export' //图标类名
            }]
            , method: 'post'
            , height: 'full-130'
            , cols: [[
                { field: 'Code', title: '编码', width: 160 }
                , { field: 'Name', title: '名称', minWidth: 120 }
                , { field: 'Year', title: '年份', width: 80 }
                , { field: 'BeginDate', title: '开始日期', width: 160 }
                , { field: 'EndDate', title: '结束日期', width: 160 }
                , { field: 'Enabled', title: '状态', align: 'center', templet: '<div>{{d.Enabled=="1"?"已启用":"未启用"}}</div>', width: 80 }
                , { field: 'right', title: '操作', width: 220, align: 'center', fixed: 'right', toolbar: '#barBtn' }
            ]]
            , page: true
            , limit: 10
            , limits: [10, 15, 20, 25, 30]
            , text: { none: '暂无数据' }
        });
        initTableBtn(table);
        //头工具栏事件
        table.on('toolbar(LAY-gp-list)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '添加'
                        , content: '/GroupPurchase/Edit'
                        , area: ["800px", ($('#LAY-gp-index').height() - 20) + "px"]
                    });
                    break;
                case 'LAYTABLE_EXPORTS':
                    var form = document.createElement('form');
                    form.action = '/GroupPurchase/GetGPListExport';
                    form.method = 'post';
                    $(document.body).append(form);
                    form.submit();
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(LAY-gp-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确认删除？', function (index) {
                    layer.close(index);
                    var index2 = layer.load(1, { time: 10 * 1000 });
                    $.ajax({
                        url: '/GroupPurchase/DelGP',
                        type: 'post',
                        data: 'gpCode=' + data.Code,
                        success: function (res) {
                            if (res.code == 0) {
                                layer.msg(res.msg, {
                                    offset: '50px'
                                    , icon: 1
                                    , time: 2000
                                }, function () {
                                    table.reload('LAY-gp-list'); //重载表格
                                });
                            } else {
                                layer.alert(res.msg);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest.status + ' ' + textStatus + ' (' + errorThrown + ')');
                        },
                        complete: function () {
                            layer.close(index2);
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '编辑'
                    , content: '/GroupPurchase/Edit?gpCode=' + data.Code
                    , area: ["800px", ($('#LAY-gp-index').height() - 60) + "px"]
                });
            }
            else if (obj.event === 'drug') {
                layer.open({
                    type: 2
                    , title: '集采药品'
                    , content: '/GroupPurchase/GroupDrug?gpCode=' + data.Code
                    , area: [($('#LAY-gp-index').width() - 60) + "px", ($('#LAY-gp-index').height() - 60) + "px"]
                });
            }
        });
        //监听批次筛选下拉框
        form.on('select(setpc)', function (data) {
            var type = data.value;
            aa = type;
            //console.log(type);
            $.ajax({
                type: 'post',
                url: '/GroupChoseDrugCompare/GetGroupInfo',
                data: "param=" + type,
                //dataType: 'json',
                success(res) {
                    var str = '<option value="ALL">全部</option>';
                    for (var i in res) {
                        str += "<option value='" + res[i].Code + "'>" + res[i].Year + " " + res[i].Name + "</option>";
                    }
                    $('#GroupPurchase').html(str)
                    form.render();
                }
            })
            //console.log(data)
        });

        //监听搜索
        form.on('submit(LAY-gp-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-gp-list', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        var $ = layui.$;

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
